using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]

    //购物车控制器
    public class ShoppingCartsController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Shopping_Cart> _shoppingCartRepository;

        public ShoppingCartsController(IConfiguration configuration, IRepository<Shopping_Cart> shoppingCartRepository)
        {
            _configuration = configuration;
            _shoppingCartRepository = shoppingCartRepository;
        }



        //获取所有购物车
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            // 分页
            var shoppingCarts = _shoppingCartRepository.Table.ToList().Skip((currentPage - 1) * pageSize).Take(pageSize); ;

            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = shoppingCarts,
                    Pager = new { currentPage, pageSize, totalSize = shoppingCarts.Count() }
                },
                Msg = "获取所有用户的购物车成功"
            };
        }

        [HttpGet("{userId}")]
        //根据用户id获取购物车
        public dynamic Get(int userId)
        {
            var currentUserShoppingCar = _shoppingCartRepository.GetById(userId);

            return new
            {
                Code = 200,
                Data = currentUserShoppingCar,
                Msg = "获取指定用户的购物车成功"
            };

        }

        //添加购物车
        // [HttpPost]
        // public dynamic Post(CURUser newAdmin)
        // {   
        //  当前用户id、当前商品id





        //将结果实例化后输出

        // }



    }
}